package com.cloudcc.mobile.search.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudcc.cloudframe.model.MenuModel;
import com.cloudcc.cloudframe.model.MenuTabModel;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.MenuManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.view.activity.BeauInfoActivity;
import com.cloudcc.mobile.view.activity.BusinesscardScanningActivity;
import com.cloudcc.mobile.view.main.MainUIActivity;
import com.cloudcc.mobile.view.schedule.ScheduleMainActivity;
import com.cloudcc.mobile.view.web.NewWebView;
import com.cloudcc.mobile.view.web.Newwebview1;
import com.litesuits.android.log.Log;
import com.mypage.model.NewSearch;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zys on 2017/5/9.
 */

public class SearchlistAdapter extends BaseAdapter {
        private List<NewSearch> mData ;
        private LayoutInflater mInflater;
        private  List<MenuTabModel> duixiang;
        private Context context;
    private String sousuo;
        public SearchlistAdapter(Context context,List<MenuTabModel> duixiang,List<MenuTabModel> lishi,String sousuo){
            this.mInflater = LayoutInflater.from(context);
            this.duixiang = duixiang;
            this.context = context;
            this.sousuo = sousuo;
            initdata(duixiang,lishi);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mData.size()>0?mData.size():0;
        }
        public void  initdata(List<MenuTabModel> duixiang,List<MenuTabModel> lishi){
            mData  = new ArrayList<NewSearch>();
            if(duixiang.size()>0){
                for (int i=0;i<duixiang.size();i++){
                    NewSearch newSearch = new NewSearch();
                    newSearch.duixiangname = duixiang.get(i).tabName;
                    newSearch.isduixiang = true;
                    newSearch.imageID = duixiang.get(i).imageId;
                    newSearch.objID = duixiang.get(i).objId;
                    newSearch.url = duixiang.get(i).url;
                    newSearch.prefix = "";
                    mData.add(newSearch);
                }
            }
            if(lishi.size()>0){
                  for (int i=0;i<lishi.size();i++){
                      NewSearch newSearch = new NewSearch();
                      newSearch.lishiname = lishi.get(i).search_name1;
                      newSearch.isduixiang = false;
                      newSearch.lishiTitle = lishi.get(i).search_name;
                      newSearch.mEntityId = lishi.get(i).search_nameID;
                      newSearch.prefix = lishi.get(i).prefix;
                      mData.add(newSearch);
                }
            }
        }
    public void changeData(List<MenuTabModel> duixiangC,List<MenuTabModel> lishiC,String sousuo) {
        if (duixiang != null) {
            duixiang.clear();
        }
        duixiang.addAll(duixiangC);
        this.sousuo = sousuo;
//        lishi.addAll(lishiC);
        initdata(duixiangC,lishiC);
        mData.size();
        notifyDataSetChanged();
    }
        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return mData.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            if (convertView == null) {
                holder=new ViewHolder();
                convertView = mInflater.inflate(R.layout.search_item, null);
                holder.textViewlishiname = (TextView) convertView.findViewById(R.id.search_lishi_name);
                holder.textViewlishineir= (TextView) convertView.findViewById(R.id.search_lishi_value);
                holder.textviewduixName= (TextView) convertView.findViewById(R.id.search_duixiang_name);
                holder.relativeLayout_duixiang= (RelativeLayout) convertView.findViewById(R.id.search_duixiang);
                holder.relativeLayout_lishi= (RelativeLayout) convertView.findViewById(R.id.search_lishi);
                holder.imageView = (ImageView) convertView.findViewById(R.id.search_duixiang_image);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder)convertView.getTag();
            }

            if(mData.get(position).isduixiang){
                holder.relativeLayout_duixiang.setVisibility(View.VISIBLE);
                holder.relativeLayout_lishi.setVisibility(View.GONE);
                holder.textviewduixName.setText(mData.get(position).duixiangname);
                holder.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.relevant_edit));
                Class drawable = R.drawable.class;
                Field field = null;
                try {
                    field = drawable.getField(mData.get(position).imageID+"_1");
                    int id = field.getInt(field.getName());
                    holder.imageView.setImageBitmap(null);
                    holder.imageView.setBackgroundResource(id);
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }else{
                holder.relativeLayout_duixiang.setVisibility(View.GONE);
                holder.relativeLayout_lishi.setVisibility(View.VISIBLE);
                //高亮显示
                try {
                    Log.d("shuju",mData.get(position).lishiname+"::"+mData.get(position).lishiTitle);
                    SpannableString ss = new SpannableString(mData.get(position).lishiname);
                    ss.setSpan( new ForegroundColorSpan(context.getResources().getColor(R.color.gaoliang)), mData.get(position).lishiname.indexOf(sousuo), mData.get(position).lishiname.indexOf(sousuo)+sousuo.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    holder.textViewlishineir.setText(ss);
                }catch (Exception ex){
                    if (null==mData.get(position).lishiname){
                        holder.textViewlishineir.setText("无主题");
                    }else {
                        holder.textViewlishineir.setText(mData.get(position).lishiname);
                    }
                }
                holder.textViewlishiname.setText(mData.get(position).lishiTitle);
//                holder.textViewlishineir.setText(mData.get(position).lishiname);

            }
            holder.relativeLayout_duixiang.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (StringUtils.isNotBlank(mData.get(position).objID)
                            && mData.get(position).objID.startsWith("android:")) {
                        if (mData.get(position).objID.startsWith("android:task")) {// 任务
                            Intent intent = new Intent(context,
                                    ScheduleMainActivity.class);
                            context.startActivity(intent);
                        } else if (mData.get(position).objID.startsWith("android:approval")) {
                            AppContext.urlString = UrlManager.getRootUrl()
                                    + "/WeiXin_getmangeAllAppRequest.action";
                            Intent intent2 = new Intent(context, NewWebView.class);
                            context.startActivity(intent2);
                        } else if (mData.get(position).objID.startsWith("android:card")) {
                            Intent intent = new Intent(context,
                                    BusinesscardScanningActivity.class);
                            context.startActivity(intent);
                        }
                    }
                    else if("report".equals(mData.get(position).prefix)){
                        Intent intent = new Intent(context, Newwebview1.class);
                        intent.putExtra("url", UrlManager.getRootUrl() + "/weixinlistRunReport.action?reportId=" + mData.get(position).objID + "&&returnId=0&&purview=1");
                        context.startActivity(intent);
                    }else{
                        MenuModel mainMenu =  MainUIActivity.getCCMenuItems();
                        Log.d("clickshanping", "闪屏测试:点击跳转第一步");
                        AppContext.indexUrl = UrlManager.getRootUrl()
                                + UrlTools.getModuleUrl(mData.get(position).objID, mData.get(position).duixiangname);
                        AppContext.urlString = UrlManager.getRootUrl()
                                + UrlTools.getTabsUrl(duixiang.get(position), mainMenu);
                        MenuManager.getInstance().enterMenuDetailOrList(duixiang.get(position),
                                (Activity) context);
                    }

                }
            });
            holder.relativeLayout_lishi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     if("report".equals(mData.get(position).prefix)){
                        Intent intent = new Intent(context, Newwebview1.class);
                        intent.putExtra("url", UrlManager.getRootUrl() + "/weixinlistRunReport.action?reportId=" + mData.get(position).mEntityId + "&&returnId=0&&purview=1");
                        context.startActivity(intent);
                    }else {
                         Intent intent = new Intent(context, BeauInfoActivity.class);
                         intent.putExtra("web", mData.get(position).mEntityId);
                         context.startActivity(intent);
                     }
                }
            });

            return convertView;
        }
        class ViewHolder{
            public TextView textViewlishiname,textViewlishineir,textviewduixName;
            public RelativeLayout relativeLayout_lishi,relativeLayout_duixiang;
            public ImageView imageView;
        }
    }
